// JavaScript Document
$(document).ready(function(e) {	
	$("#jointName").hide();
	$("#manualDiv").hide();

	var loanamount=$("#loanamountTd").text(); loanamount = toNumber(loanamount);
	var homevalue=$("#homevalueTd").text(); homevalue = toNumber(homevalue);
	if(loanamount>=10000 && homevalue!=0 && homevalue!=""){	 //loan amount >=10000 and own property, Manual
		$("#manualDiv").show();
	}
	if($("#appnameTd").text()!=""){
		$("#jointName").show();
		$("#manualDiv").show();
	}
	
		/////////////////////////////////calculate credit/////////////////////////////////////////
		$("#creditTd").html("<span style='color:green'><b>Pass</b></span>");
		var pp1=pp2="Fail";
		
		pp1="Pass";
		var ch1=$("#chistory1").text();	
		var ch2=$("#chistory2").text();
		if(ch1!="Clean (no default)" && ch1!="1 default" && ch1!="2 defaults" && ch1!="Ex-bankrupt" && ch1!="Past Part IX debt agreement") pp1="Fail"; 
		if(ch1=="1 default" || ch1=="2 defaults"){			//check if pp has 1/2 defaults
			var totalDA=0;
			var p1ds1=$("#p1ds1").text();
			var p1ds2=$("#p1ds2").text();
			var p1da1=$("#p1da1").text(); p1da1 = toNumber(p1da1);
			var p1da2=$("#p1da2").text(); p1da2 = toNumber(p1da2);
			if(p1ds1=="Paying" || p1ds1=="Outstanding") totalDA+=p1da1;
			if(p1ds2=="Paying" || p1ds2=="Outstanding") totalDA+=p1da2;
			if(totalDA>10000) 
				pp1="Fail";
		}
				
			
		if(pp1=="Fail" && $("#filledformTd").text()=="No"){
			pp2="Pass";	
			
			if(ch2!="Clean (no default)" && ch2!="1 default" && ch2!="2 defaults" && ch2!="Ex-bankrupt" && ch2!="Past Part IX debt agreement") pp2="Fail"; 
			if(ch2=="1 default" || ch2=="2 defaults"){
				var totalDA=0;
				var p2ds1=$("#p2ds1").text();
				var p2ds2=$("#p2ds2").text();
				var p2da1=$("#p2da1").text(); p2da1 = toNumber(p2da1);
				var p2da2=$("#p2da2").text(); p2da2 = toNumber(p2da2);
				if(p2ds1=="Paying" || p2ds1=="Outstanding") totalDA+=p2da1;
				if(p2ds2=="Paying" || p2ds2=="Outstanding") totalDA+=p2da2;
				if(totalDA>10000) 					
					pp2="Fail";
			}

		}
		
		if(homevalue!=0){
			if(ch1!="Under bankruptcy" && ch1!="Under Part IX debt agreement")
				pp1="Pass";
			if(ch2!="" && ch2!="Under bankruptcy" && ch2!="Under Part IX debt agreement")
				pp2="Pass";	
		}
	
		if(pp1=="Fail" && pp2=="Fail"){
			$("#creditTd").html("<span style='color:red'><b>Support Required</b></span>");	
			$("#credittitle").html("<span style='color:red'><b>Support Required</b></span>");	
		}
		else{
			$("#creditTd").html("<span style='color:green'><b>Pass</b></span>");
			$("#credittitle").html("<span style='color:green'><b>Pass</b></span>");	
		}
		if(document.getElementById("manualDiv").style.display!='none'){
			$("#creditTd").html("<span>N/A</span>");
			$("#credittitle").html("<span>N/A</span>");	
		}
		
		///////////////////calculate asset///////////////////
		$("#assetTd").html("<span style='color:green'><b>Pass</b></span>");
		$("#assettitle").html("<span style='color:green'><b>Pass</b></span>");
		if(homevalue!=0 && homevalue!=""){
			$("#eaTd").text("Property - up to $9,000");
		}//endif
		else if($("#vehicleValue").text()!="" && $("#vehicleValue").text()!=0 ){
			var d=$("#desiredAmount").text(); d = d.replace(/\,/g,'');
			var n=$("#vehicleValue").text(); n = n.replace(/\,/g,'');
			if(n<=3000){
					if(d>1000)
						$("#eaTd").text("Vehicle: $1000-$2000");
					else
						$("#eaTd").text("Vehicle: $"+d);
			}
			else if(n<10000){
				n=n/2;
				var minValue=Math.round((n-1000)/1000)*1000; if(minValue<1000) minValue=1000;
				var maxValue=Math.round((n+1000)/1000)*1000;
				if(d>minValue)
					$("#eaTd").text("Vehicle: $" + minValue + "-$" + maxValue);
				else
					$("#eaTd").text("Vehicle: $"+d);	
			}
			else{
				if(d>5000)
					$("#eaTd").text("Vehicle: $5000");
				else
					$("#eaTd").text("Vehicle: $"+d);
			}
		}//end elseif
		else{  //no real estate, no vehicle
			$("#eaTd").text("None");
			$("#assetTd").html("<span style='color:red'><b>Support Required</b></span>");
			$("#assettitle").html("<span style='color:red'><b>Support Required</b></span>");
		}//end else
		
		var v1f=$("#v1finance").text();
		var v2f=$("#v2finance").text();
		if(v1f!="No" && v2f!="No" && homevalue==0 && homevalue==""){
			$("#assetTd").html("<span style='color:red'><b>Support Required</b></span>");
			$("#assettitle").html("<span style='color:red'><b>Support Required</b></span>");
		}
		
		
		
	if(loanamount>=10000 && homevalue!=0 && homevalue!=""){	 //loan amount >=10000 and own property, Manual
		$("#eaTd").text("N/A");
	}
	if($("#appnameTd").text()!=""){ //joint app, manual
		$("#eaTd").text("N/A");
	}
	
});
function toNumber(str){
	if(str=="")
		str=0;
	else{
		str=str.replace(/\,/g,'');
		str=parseInt(str);	
	} 
	return str;
}